package com.manbearpig.todue;

import java.util.ArrayList;

import com.manbearpig.todue.R;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This list activity displays the todo items
 * @author Brandon Devereaux
 * @version 1.0
 *
 */
public class ToDoListActivity extends ListActivity
{
	String username = "";
	boolean filterByType;
	boolean filterByDate;
	boolean filterByComplete;
	boolean filterBefore;
	boolean filterIncomplete;
	
	String filterCategory = "";
	String filterDate = "";	
	
	DatabaseHelper dbh;
	SQLiteDatabase db;
	Cursor cursor;
	
	/**
	 * Method called when the activity is created
	 */
	public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todoscreen);
        Bundle bundle = this.getIntent().getExtras();
        this.username = (String)bundle.get("Username");
        this.filterByType = bundle.getBoolean("filterByType");
        this.filterByDate = bundle.getBoolean("filterByDate");
        this.filterByComplete = bundle.getBoolean("filterByComplete");
        this.filterBefore = bundle.getBoolean("filterBefore");
        this.filterIncomplete = bundle.getBoolean("filterIncomplete");
        this.filterCategory = (String)bundle.getString("filterCategory");
        this.filterDate = (String)bundle.getString("filterDate");
        
        String queryString = "SELECT _id, Short_Description, Due_Date, Completed FROM Todos WHERE Username=?";
        if(filterByType)
        	queryString += " and Category='"+filterCategory+"'";
        if(filterByComplete)
        {
        	if(filterIncomplete)
        		queryString += " and Completed='Not Completed'";
        	else
        		queryString += " and Completed='Completed!'";
        }
        if(filterByDate)
        {
        	if(filterBefore)
        		queryString += " and Due_Date < Datetime('"+filterDate+"')";
        	else
        		queryString += " and Due_Date > Datetime('"+filterDate+"')";
        }
        dbh = new DatabaseHelper(this);
        db = dbh.getReadableDatabase();
        db.beginTransaction();
        cursor = db.rawQuery(queryString,new String[]{username});
        // and Due_Date >= Datetime('2012-03-31')
        db.setTransactionSuccessful();
        db.endTransaction();
        startManagingCursor(cursor);
        
        String[] columns = new String[] {"Short_Description", "Due_Date","_id","Completed"};
        // the XML defined views which the data will be bound to
        int[] to = new int[] { R.id.description, R.id.dueDate, R.id._ID, R.id.completeText };
       
        // create the adapter using the cursor pointing to the desired data as well as the layout information
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.todoitem, cursor, columns, to);

        // set this adapter as your ListActivity's adapter
        this.setListAdapter(mAdapter);
        
        ListView lv = getListView();
        lv.setClickable(true);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
                onListItemClick(v,pos,id);
            }
        });
        
        
    }
	
	/**
	 * Method called on item click - edits the item
	 * @param v the requested view
	 * @param pos the items position in the list
	 * @param id the requested id
	 */
	protected void onListItemClick(View v, int pos, long id) {
		Cursor cur = (Cursor)getListView().getItemAtPosition(pos);
		int value = Integer.parseInt(cur.getString(cur.getColumnIndex("_id")));
	    Intent myIntent = new Intent(v.getContext(), AddToDueActivity.class);
	    myIntent.putExtra("Username", this.username);
	    //int value = Integer.parseInt((String)((TextView)findViewById(R.id._ID)).getText());
	    myIntent.putExtra("_ID", value);
	    cur.close();
	    stopManagingCursor(cursor);
	    cursor.close();
	    db.close();
	    dbh.close();
	    finish();
	    startActivity(myIntent);
	}
	
	/**
	 * OnClickListener to create the Todo
	 * @param view the requested view
	 */
	public void createTodoView(View view)
	{
		Intent myIntent = new Intent(view.getContext(), AddToDueActivity.class);
		myIntent.putExtra("Username", this.username);
        startActivityForResult(myIntent, 0);
	}
	
	/**
	 * OnClickListener to add a filter
	 * @param view the requested view
	 */
	public void addFilter(View view)
	{
	    Intent myIntent = new Intent(view.getContext(), FilterActivity.class);
	    myIntent.putExtra("Username", this.username);
	    stopManagingCursor(cursor);
	    cursor.close();
	    db.close();
	    dbh.close();
	    finish();
	    startActivity(myIntent);
	}
	
	
	
	
	
	/**
	 * OnClickListener for sign out
	 * @param view the requested view
	 */
	public void signout(View view)
	{
		db.close();
		dbh.close();
		finish();		
	}
	
}
